#!coding=utf-8
from standard_function import *


def clickappdown(driver):
        try:
            #点击安装按钮
            btns = WaitforElementsById(driver,"com.liqucn.android:id/root",30)
        except NoSuchElementException:
            raise NoSuchElementException
        else:
            for i in range(0,7):
                print i
                item_btn=btns[i]
                try:
                    size=WaitforElementById(item_btn,"com.liqucn.android:id/size",30).get_attribute("text")
                except NoSuchElementException:
                    continue
                print size
                if 'G' not in size:
                    if 'K' in size or float(size[0:-2])<20:
                        continue
                try:
                    down_btn=WaitforElementById(item_btn,"com.liqucn.android:id/download_button",30)
                except NoSuchElementException:
                    continue
                if down_btn.get_attribute("name")!=u'安装':
                    continue
                down_btn.click()
                waittime=40
                while waittime:
                    try:
                        text = WaitforElementById(item_btn,"com.liqucn.android:id/download_speed_text",5).get_attribute("name")
                    except NoSuchElementException:
                        down_btn.click()
                        #swipeUp(driver,0.9,0.8)
                        break
                    print text
                    if '0.0%' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"


def soft(driver):
    try:
        WaitforElementById(driver,'com.liqucn.android:id/home_tab_careful',30).click()
        WaitforElementByName(driver,u"分类",30).click()
        WaitforElementByName(driver,u"软件",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'通讯',u'同步',u'办公',u'安全',u'购物',u'教育',u'阅读',u'理财',u'音乐',u'视频',u'摄影',u'社交',u'新闻',u'网络',u'输入',u'美化',u'系统',u'交通',u'娱乐',u'健康',u'天气',u'生活',u'刷机']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                print "go back"
                driver.back()
            except NoSuchElementException:
                driver.back()
                continue
    #driver.back()

def game(driver):
    try:
        #WaitforElementById(driver,'com.liqucn.android:id/home_tab_careful',30).click()
        #WaitforElementByName(driver,u"分类",30).click()
        WaitforElementByName(driver,u"游戏",30).click()
    except NoSuchElementException:
        raise NoSuchElementException
    gamecatetory= [u'益智',u'棋牌',u'冒险',u'动作',u'射击',u'策略',u'体育',u'养成',u'角色',u'休闲',u'经营']
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
                print "go back"
                driver.back()
            except NoSuchElementException:
                driver.back()
                continue
    driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.liqucn.android'    # apk的包名
    desired_caps['appActivity'] = '.ui.activity.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(2)
    #del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()
